package com.clas.liu.myapplication.view.activity.impl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.info.Info;
import com.clas.liu.myapplication.model.entity.info.InfoData;
import com.clas.liu.myapplication.presenter.impl.CategoryPresenter;
import com.clas.liu.myapplication.view.activity.i.ICategoryActivity;
import com.clas.liu.myapplication.view.adapter.NormalInfoAdapter;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.util.SomeResources;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 2019/5/8.
 */

public class CategoryActivity extends BaseActivity<CategoryPresenter> implements ICategoryActivity {


    private ImageView back;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private TextView title;
    private NormalInfoAdapter adapter;
    private LinearLayoutManager layoutManager;

    private int page_no = 1;
    private final int page_size = 10;
    private List<Info> infos;
    private boolean isRefresh = true;
    private boolean isAll = false;
    private int lastVisibleItem;
    private String categoryStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_layout);
        findView();
        init();
        setListener();
    }

    public void infoRefresh() {
        if (NetworkHelper.isNetworkAvailable(this)) {
            page_no = 1;
            isRefresh = true;
            mPresenter.getInfo(getIntent().getIntExtra("category", 4), page_no, page_size);
        } else {
            refreshLayout.setRefreshing(false);
            showToast("亲亲，网好像断了呢!~", Toast.LENGTH_SHORT);
        }
    }


    private void setListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                infoRefresh();
            }
        });
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (adapter != null && newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount()) {
                    if (!isAll)
                        mPresenter.getInfo(getIntent().getIntExtra("category", 4), page_no, page_size);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void init() {
        categoryStr = SomeResources.getCategorys().get(getIntent().getIntExtra("category", 4));
        title.setText(categoryStr);
        mPresenter.getInfo(getIntent().getIntExtra("category", 4), page_no, page_size);
        refreshLayout.setRefreshing(true);
    }

    private void findView() {
        back = findViewById(R.id.category_back_bt);
        refreshLayout = findViewById(R.id.category_refresh);
        recyclerView = findViewById(R.id.category_rv);
        title = findViewById(R.id.category_title);
    }

    @Override
    public void failBecauseNetReturn(int code) {
        refreshLayout.setRefreshing(false);
        showToast(code + " 请求失败!", Toast.LENGTH_SHORT);
    }

    @Override
    public void InfoReturn(int code, InfoData data) {
        if (infos == null) {
            infos = new ArrayList<>();
        }
        if (isRefresh) {
            infos.clear();
            isRefresh = false;
        }
        infos.addAll(data.getData());
        if (isRefresh) {
            if (adapter != null)
                adapter.deleteLoadingMore(false);
        }
        recyclerView.setVisibility(View.VISIBLE);
        refreshLayout.setRefreshing(false);
        if (layoutManager == null) {
            layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
        }
        if (adapter == null) {
            adapter = new NormalInfoAdapter(this, infos);
            recyclerView.setAdapter(adapter);
        } else
            adapter.update(infos);
        if (data.getPageNum() == data.getPages()) {
            adapter.deleteLoadingMore(true);
            isAll = true;
        } else if (data.getPageNum() < data.getPages()) {
            page_no++;
            isAll = false;
        }

    }

    @Override
    protected CategoryPresenter getPresenter() {
        return new CategoryPresenter(this);
    }
}
